package java_1_10.interface_test;

import java.util.Arrays;

public class Student implements Comparable<Student>{//<>是一个泛型，就是类型的意思
    private String name;
    private int age;
    public Student(String name,int age){
        this.name=name;
        this.age=age;
    }
//自己设置内部，按什么比较，这里假设按年龄比较
    @Override
    public int compareTo(Student o) {//年龄按升序排列，小的在前。若想要降序排列，<时return 1,>时return -1
        if(this.age==o.age){
            return 0;
        }else if(this.age<o.age){
            return -1;
        }else{
            return 1;
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
class Tests{
    public static void main(String[] args) {
        Student stu1=new Student("图图",5);
        Student stu2=new Student("慢羊羊",10);
        Student stu3=new Student("大大怪",30);
        Student[] students={stu1,stu2,stu3};
        Arrays.sort(students);
        System.out.println(Arrays.toString(students));
    }
}
